#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAX (1024*10)
void HandleCgiCalc(char **output)
{
  const char* method;
  method = getenv("ENV_METHOD");
  char body[MAX]={0};
  if(method == NULL)
  {
      fprintf(stderr,"获取method失败\n");
      return;
  }
  else if(strcmp(method,"GET")== 0)
  {
     *output = getenv("ENV_QUERYSTRING");
  }
  else{
    int contentlen = atoi(getenv("ENV_CONTENTLEN"));
    int i=0;
    for(;i<contentlen;i++){
      read(0,&body[i],1);
    }
    *output = body;
  }
}
int main()
{
  fprintf(stderr,"进入cgi程序\n");
  char *query_string;
  HandleCgiCalc(&query_string);
  fprintf(stderr,"得到query_string:%s",query_string);
  int a,b;
  sscanf(query_string,"a=%d&b=%d",&a,&b);
  printf("<html><h1>a+b=%d</h1></html>",a+b);
  return 0;
}
